package pl.edu.mimuw.irs.rcp.wizard.newproject;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.wizard.IWizardPage;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Text;

import pl.edu.mimuw.irs.rcp.Activator;
import pl.edu.mimuw.irs.rcp.util.MessageCode;
import pl.edu.mimuw.irs.rcp.util.UITexts;
import pl.edu.mimuw.irs.rcp.wizard.NewProjectWizard;

public class ProjectPage extends WizardPage implements Listener {

	// *** Attributes

	private Status okStatus = new Status(IStatus.OK, Activator.PLUGIN_ID, 0, "", null);
	private Label lblProjectName;
	private Text txtProjectName;
	private Status txtProjectNameStatus;
	
	// *** Constructors

	public ProjectPage() {
		super(UITexts.get(MessageCode.PROJECT_WIZARD_PROJECT_NAME));
		setPageComplete(false);
	}
	
	// *** Methods

	@Override
	public void createControl(Composite parent) {
		GridData gridData;
		Composite composite =  new Composite(parent, SWT.NULL);

	    // create the desired layout for this wizard page
		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 2;
		composite.setLayout(gridLayout);
		
		gridData = new GridData();
		gridData.horizontalAlignment = GridData.BEGINNING;	
		
		lblProjectName = new Label(composite, SWT.NONE);
		lblProjectName.setText(UITexts.get(MessageCode.PROJECT_WIZARD_PROJECT_NAME));				
		txtProjectName = new Text(composite, SWT.BORDER);
		gridData = new GridData(GridData.FILL_HORIZONTAL);
		//gridData.horizontalSpan = ncol - 1;
		txtProjectName.setLayoutData(gridData);
		
		// add descripiton and title
		setErrorMessage(null);
		setTitle(UITexts.get(MessageCode.PROJECT_WIZARD_PROJECT_TITLE));
		setMessage(UITexts.get(MessageCode.PROJECT_WIZARD_PROJECT_INTRO));
		
		setControl(composite);		
		addListeners();
	}
	
	private void addListeners() {
		txtProjectName.addListener(SWT.KeyUp, this);
	}
	
	@Override
	public void handleEvent(Event event) {
	    Status status = okStatus;
	    // If the event is triggered by the destination or departure fields
	    // set the corresponding status variable to the right value
	    if ((event.widget == txtProjectName)) {
	        if (txtProjectName.getText() == null || txtProjectName.getText().isEmpty())
	            status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, 0, 
	            		lblProjectName.getText() + " - " + 
	            		UITexts.get(MessageCode.WIZARD_FIELD_EMPTY), null);        
	        txtProjectNameStatus = status;
	    }

	    // Show the most serious error
	    IStatus mostSevere = findMostSevere();
	    applyToStatusLine(mostSevere);
	    if (mostSevere == okStatus) {
	    	saveDataToModel();
	    	setPageComplete(true);
	    } else {
	    	setPageComplete(false);
	    }
		getWizard().getContainer().updateButtons();
	}

	private void applyToStatusLine(IStatus status) {
		String message= status.getMessage();
		if (message.length() == 0) message = null;
		switch (status.getSeverity()) {
			case IStatus.OK:
				setErrorMessage(null);
				setMessage(message);
				break;
			case IStatus.WARNING:
				setErrorMessage(null);
				setMessage(message, WizardPage.WARNING);
				break;				
			case IStatus.INFO:
				setErrorMessage(null);
				setMessage(message, WizardPage.INFORMATION);
				break;			
			default:
				setErrorMessage(message);
				setMessage(null);
				break;		
		}
	}	
	
	private IStatus findMostSevere() {
		if (txtProjectNameStatus.matches(IStatus.ERROR)) {
			return txtProjectNameStatus;
		} else {
			return txtProjectNameStatus;
		}
	}

	public boolean canFlipToNextPage()
	{
		//if (getErrorMessage() != null) return false;
		//if (isTextNonEmpty(txtProjectName))
		//	return true;
		return false;
	}
	
	/*private static boolean isTextNonEmpty(Text t) {
		String s = t.getText();
		if ((s!=null) && (s.trim().length() >0)) return true;
		return false;
	}*/
	
	public IWizardPage getNextPage() {
		saveDataToModel();
		return super.getNextPage();
	}
	
	private void saveDataToModel() {
	    // Gets the model
		NewProjectWizard wizard = (NewProjectWizard)getWizard();
		ProjectModel model = wizard.getNewProjectModel();

		model.projectName = txtProjectName.getText();
	}
	
}
